package com.factory.iamp.dao;

import com.alibaba.fastjson.JSONObject;
import com.factory.iamp.pojo.QuestionLog;
import com.factory.iamp.pojo.TableContent;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TableContentMapper {
    @Select("select count(*) from question_log where tid = #{tid} and oid = #{oid}")
    int countProblemLog(@Param("tid") int tid, @Param("oid") int oid);

    @Select("select * from table_content where cid = #{cid} limit 1")
    TableContent findById(@Param("cid") int cid);

    @Select("select * from table_content where tid = #{tid} and oid = #{oid} limit 1")
    TableContent findByTidOid(@Param("tid") int tid, @Param("oid") int oid);

    @Select({
            "<script>",
            "select * from table_content where oid in (",
            "<foreach collection = 'idList' item = 'item' index = 'index' separator = ','>",
            "#{item}",
            "</foreach>)",
            " and tid = #{tid}",
            "</script>"
    })
    List<TableContent> findByIdList(@Param("idList") List<Integer> idList, @Param("tid") int tid);

    @Select({
            "select content, Scheck, Ocheck, Zcheck, problem ",
            "from table_content left join table_template_body on table_content.oid = table_template_body.oid ",
            "left join select_list on table_content.oid = select_list.id ",
            "where table_content.tid = #{tid} order by select_list.orderid"
    })
    List<JSONObject> findValueByIdList(@Param("tid") int tid);

    @Select("select * from question_log where tid = #{tid} and oid = #{oid} and recorder = #{uid}")
    QuestionLog findQuestionLog(@Param("tid") int tid, @Param("oid") int oid, @Param("uid") String uid);

    @Select("select record from question_log where tid = #{tid} and oid = #{oid} limit 1")
    String getDumpQuestionLog(@Param("tid") int tid, @Param("oid") int oid);

    @Insert("insert into table_content(tid, oid, content, Scheck, Ocheck, Zcheck, created_at, updated_at) values " +
            "(#{tid}, #{oid}, #{content}, #{Scheck}, #{Ocheck}, #{Zcheck}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "cid", keyColumn = "cid")
    void addTableContent(TableContent tableContent);

    @Insert({
            "insert into question_log(tid, oid, list, recorder, record, created_at) ",
            "values (#{tid}, #{oid}, #{list}, #{uid}, #{record}, NOW())"
    })
    void addProblemLog(
            @Param("tid") int tid, @Param("oid") int oid,
            @Param("uid") String uid, @Param("list") String list, @Param("record") String record
    );

    @Update("update table_content set content = #{content}, updated_at = NOW() where cid = #{cid}")
    void alterTableContent(@Param("cid") int cid, @Param("content") String content);

    @Update("update `table` set header_value = #{content}, updated_at = NOW() where tid = #{tid}")
    void alterTableHead(@Param("tid") int tid, @Param("content") String content);

    @Update("update table_content set ${name} = #{path}, updated_at = NOW() where cid = #{cid}")
    void addTableContentCheck(@Param("cid") int cid, @Param("name") String name, @Param("path") String path);

    @Update("update table_content set problem = #{problem}, updated_at = NOW() where tid = #{tid} and oid = #{oid}")
    void addTableContentProblem(@Param("tid") int tid, @Param("oid") int oid, @Param("problem") String problem);

    @Update({
            "update question_log set record = #{record}, sign = #{sign}, signer = #{uid}, updated_at = NOW() ",
            "where tid = #{tid} and oid = #{oid}"
    })
    void processProblemLog(
            @Param("tid") int tid, @Param("oid") int oid, @Param("uid") String uid,
            @Param("record") String record, @Param("sign") String sign
    );

    @Update("update table_content set problem = null where tid = #{tid} and oid = #{oid}")
    void removeTableQuestion(@Param("tid") int tid, @Param("oid") int oid);

    @Update("update table_content set problem = #{problem} where tid = #{tid} and oid = #{oid}")
    void updateTableQuestion(@Param("tid") int tid, @Param("oid") int oid, @Param("problem") String problem);

    @Update({
            "update question_log set signer = null, sign = 'need', updated_at = null ",
            "where tid = #{tid} and oid = #{oid} and signer = #{uid}"
    })
    void removeTableQuestionSign(@Param("tid") int tid, @Param("oid") int oid, @Param("uid") String uid);

    @Delete("delete from question_log where tid = #{tid} and oid = #{oid}")
    void deleteDumpQuestionLog(@Param("tid") int tid, @Param("oid") int oid);

    @Delete("delete from question_log where tid = #{tid} and oid = #{oid} and recorder = #{uid}")
    void deleteQuestionLog(@Param("tid") int tid, @Param("oid") int oid, @Param("uid") String uid);
}
